/*********************************************************
*  @file    cm_main.c
*  @brief   ML302 OpenCPU Entry File
*  Copyright (c) 2019 China Mobile IOT.
*  All rights reserved.
*  created by XieGangLiang 2019/10/08
********************************************************/
#ifdef CM_DEMO_SUPPORT

#include "cm_main.h" 
osThreadId OC_Main_TaskHandle;
extern osMutexId cmd_mutex;
extern unsigned char * cmd_buf[20]; //最多接受20个参数
extern int cmd_len;

cm_cmd_t cmd_vector[] = {
            {"GPIO_READ" , cm_test_gpio_read}, //CM:GPIO_READ:n
            {"GPIO_WRITE", cm_test_gpio_write},//CM:GPIO_WRITE:n:delay:loop
            {"GPIO_IRQ"  , cm_test_gpio_irq},  //CM:GPIO_IRQ:n:INTERRUPT_ON/INTERRUPT_OFF
			{"GPIO_DEINIT" , cm_test_gpio_deinit}, //CM:GPIO_DEINIT:n
            {"FORCEDL"   , cm_force_download}, //CM:FORCEDL
            {"I2C"       , cm_test_i2c_bmp180},//CM:I2C:bps
            {"TCPCLIENT" , cm_test_tcpclient}, //CM:TCPCLIENT:106.54.97.79:2019
            {"TCPCLIENTSEND" , cm_test_tcpclient_send}, //CM:TCPCLIENTSEND:DATA
            {"UDP"       , cm_test_udp},       //CM:UDP:106.54.97.79:2019
            {"ADC"       , cm_test_adc},       //CM:ADC:n
            {"VBAT"       , cm_test_vbat},       //CM:VBAT
            {"RP_ALARM"  , cm_test_set_repeate_alarm}, //CM:RP_ALARM:0:1:2:14:20:5  在周天、周一、周二的14点20分5s提示
            {"SG_ALARM"  , cm_test_single_time_alarm}, //CM:SG_ALARM:50  50s后提醒
            {"VIRT_AT"   , cm_test_send_at},   //CM:VIRT_AT:AT+CGACT?
            {"DUMP_AT"   , cm_test_dump_at},   //CM:DUMP_AT
            {"REBOOT"    , cm_test_reboot},    //CM:REBOOT
#ifdef  CM_HTTP_SUPPORT
            {"HTTP"      , cm_test_http},      //CM:HTTP:n:www.baidu.com
#endif
#ifdef  CM_EDP_SUPPORT
            {"EDP"       , cm_test_edp},       //CM:EDP
#endif
#ifdef  CM_ALIYUN_SUPPORT
            {"ALIYUN"    , cm_test_aliyun},    //CM:ALIYUN
#endif
#ifdef  CM_MQTT_SUPPORT                        //CM:MQTT:server:port:client_id:keepAlive:user:passwd:clean:version:encrypt:rec:subtopic:subqos:pubtopic:pubqos:pubdup:pubmessage
            {"MQTT"      , cm_test_mqtt},      //CM:MQTT:1  MQTTS;    CM:MQTT:0  MQTT;
#endif
#ifdef  CM_DM_SUPPORT
            {"DM"        , cm_test_dm_register}, 
#endif

#ifdef  CM_FOTA_SUPPORT
            {"ONENET_FOTA_DEV"   , cm_test_onenet_fota_setdevinfo},    //CM:ONENET_FOTA_DEV
            {"ONENET_FOTA"       , cm_test_onenet_fota},       //CM:ONENET_FOTA
            {"ONENET_FOTA_DEL"       , cm_test_onenet_fota_deletetask},       //CM:ONENET_FOTA_DEL
#endif

#ifdef CM_GNSS_SUPPORT
            {"LBS"       , cm_test_lbs},       //CM:LBS
            {"GNSS_ENABLE"       , cm_test_gnss_enable},       //CM:GNSS_ENABLE
            {"GNSS_LOCATE"       , cm_test_gnss_getlocateinfo},       //CM:GNSS_LOCATE
            {"GNSS_RAW"       , cm_test_gnss_getrawdata},       //CM:GNSS_RAW
#endif

            {"FWRITE"    , cm_test_write_file}, //CM:FWRITE:TESTMESSAGE
            {"FREAD"     , cm_test_read_file},  //CM:FREAD
            {"DATE"      , cm_test_get_date},   //CM:DATE
            {"MD5"       , cm_test_md5},        //CM:MD5:test_message
            {"BASE64"    , cm_test_base64},        //CM:BASE64:test_message
            {"SHA"       , cm_test_sha},        //CM:SHA:test_message
            {"CJSON"     , cm_test_cjson}, 
#ifdef CM_LCD_SUPPORT
			{"LCD"     , cm_test_lcd}, //CM:LCD:n
#endif			
#ifdef CM_TTS_SUPPORT
			{"TTS"   	 , cm_test_tts},//CM:TTS:str:coding
#endif
#ifdef CM_AUDIO_SUPPORT
			{"AUDIO"   	 , cm_test_audio},//CM:AUDIO:operation:param
#endif
};
void cm_main_task(void *p)
{  
   unsigned char buf[50] = {0};
   int i;
   struct l_tm t;
   cm_test_uart_init();
   cm_printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
   cm_printf("ML302 OpenCPU Starts\n");
   cm_printf("Boot Cause:%d\n",cm_sys_get_boot_cause());
   cm_sys_get_sdk_swver(buf,50);
   cm_printf("SDK VERSION:%s\n",buf);
   cm_sys_get_base_swver(buf,50);
   cm_printf("BASELINE VERSION:%s\n",buf);
   cm_sys_get_hwver(buf,50);
   cm_printf("HW VERSION:%s\n",buf);  
   cm_printf("waiting for network...\n");
   cm_test_vir_at_init(); 
   cm_test_network_config();
   time_to_date(osiEpochSecond()+cm_get_timezone()* 15*60,&t);
   cm_printf("Now:%d-%d-%d:%d:%d:%d\n",t.tm_year,t.tm_mon,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec);
   cm_test_get_imei();
   cm_test_get_imsi();
   cm_test_get_iccid();
   #ifdef CM_FOTA_SUPPORT   //启用OneNet FOTA不可删除
    char *version = NULL;
    onVersion(&version);
    cm_printf("OneNet FOTA version:%s\n", version);

    #endif
    while(1)
    {
        cm_printf("\nplease input cmds:\n");
        osSignalWait(0x0004, osWaitForever);

        if((cmd_len < 2))
        {
            cm_printf("CMD NOT DEFINE\n");    
        }
        else
        {
            for(i = 0;i< (sizeof(cmd_vector)/sizeof(cm_cmd_t));i++)
            {                
                if(strcmp(cmd_buf[1],cmd_vector[i].cmdstr) == 0)
                {
                    (*(cmd_vector[i].cmdfunc))(cmd_buf,cmd_len);
                    cm_printf("OK\n");
                    break;
                }
            }
            if( i >=  (sizeof(cmd_vector)/sizeof(cm_cmd_t)))
            {
                cm_printf("CMD NOT DEFINE\n");
            }
        }  
        osMutexRelease( cmd_mutex );
    }
}

#endif

void ML302_OpenCPU_Entry()
{
#ifdef CM_DEMO_SUPPORT
    // cm_test_alarm_init();
    // cm_test_keypad_init();
    // osThreadDef(OC_Main_Task, cm_main_task, osPriorityNormal, 0, 8192);
    // OC_Main_TaskHandle = osThreadCreate(osThread(OC_Main_Task), 0);  

#ifdef CM_FOTA_SUPPORT   //启用OneNet FOTA不可删除
    cm_onenet_fota_regcbex();
#endif

#endif



// 万物以网的业务
#ifdef CM_ATOL_E10E_SUPPORT
    atol_main_init();
#endif


}
//SDK中调用，不可删除
int cm_wdt_config_cb(void)
{
    return 0;//关闭看门狗
    //return 1;//开启看门狗
}
